<?php
echo $this->load->view ( 'header' );
?>
<!--头部开始-->
<div>
	<ul class="breadcrumb">
		<li><a href="<?php echo site_url('main'); ?>"><?php echo lang('set_system'); ?></a><span
			class="divider">/</span></li>
		<li><a
			href="<?php echo site_url($this -> config -> item('admin_folder').'role'); ?>"><?php echo $title; ?></a></li>
	</ul>
</div>
<!--头部结束-->
<?php
if ($this->session->flashdata ( 'message' )) {
	$message = $this->session->flashdata ( 'message' );
}
if ($this->session->flashdata ( 'error' )) {
	$error = $this->session->flashdata ( 'error' );
}
if (function_exists ( 'validation_errors' ) && validation_errors () != '') {
	$error = validation_errors ();
}
?>
		
		<?php if (!empty($message)): ?>
<div class="alert alert-info">
	<a class="close" data-dismiss="alert">×</a>
			<?php echo $message; ?>
		</div>
<?php endif; ?>
						<?php if (!empty($error)): ?>
<div class="alert alert-error">
	<a class="close" data-dismiss="alert">×</a>
			<?php echo $error; ?>
		</div>
<?php endif; ?>

<!--角色列表开始-->
<div class="row-fluid">
	<div class="box span12">
		<div class="box-header well">
			<h2>
				<i class="icon-info-sign"></i> 管理项目文件-<?php echo $productName; ?></h2>
			<div class="box-icon">
				<a href="#" class="btn btn-minimize btn-round"><i
					class="icon-chevron-up"></i></a> <a href="#"
					class="btn btn-close btn-round"><i class="icon-remove"></i></a>
			</div>
		</div>
		<div class="box-content">
			<div class="span5" style="height:500px;overflow:auto">
				<table
				class="table tree">
				<tbody>
                <?php if($datas):?>
					<?php foreach($datas as $n => $menu_datas): ?>

						<?php if($menu_datas['pid']==0):?>

						<tr class="treegrid-<?php echo $menu_datas['id']; ?> expand_menu">
							<td data="<?php echo  $menu_datas['id'];?>"><?php echo $menu_datas['name_cn']; ?> (<span class="count"><?php echo $menu_datas['count']; ?></span>)</td>
							<td>
								<a></a>
							</td>
						</tr>		
					                <?php else:?>
					        <?php if(!isset($menu_datas['have_child'])):?>
					    <tr class="treegrid-<?php echo $menu_datas['id']; ?> treegrid-parent-<?php echo $menu_datas['pid']; ?>">
							<td class="menus" data="<?php echo  $menu_datas['id'];?>"><?php echo $menu_datas['name_cn']; ?>(<span class="count"><?php echo $menu_datas['count']; ?></span>)</td>
							<td>
								<a
								class="addmenu"
							dataname="<?php echo $menu_datas['name_cn']; ?>"
							data="<?php echo $menu_datas['id']; ?>"
							title="<?php echo lang('menu_edit'); ?>"><span
								title="<?php echo lang('menu_edit'); ?>"
								class="icon icon-plus"></span></a>
								<?php else:?>
						<tr class="treegrid-<?php echo $menu_datas['id']; ?> expand_menu treegrid-parent-<?php echo $menu_datas['pid']; ?>">
							<td data="<?php echo  $menu_datas['id'];?>"><?php echo $menu_datas['name_cn']; ?>(<span class="count"><?php echo $menu_datas['count']; ?></span>)</td>
							<td>
								<a><span></span></a>
                <?php endif;?>

							</td>
						</tr>	
                <?php endif;?>

					<?php endforeach; ?>
			</div>
			
                <?php else:?>
                    <tr><td colspan="4"><div class="center"><h4>无任何数据</h4></div></td></tr>
                <?php endif;?>
			</table>
		</div>

		<div class="span7">
			<table
				class="table table-striped table-bordered bootstrap-datatable datatable">
				<thead>
					<tr>
						<th>文件名称</th>
						<th>文件大小</th>
						<th><?php echo lang('product_handle'); ?></th>
					</tr>
				</thead>
				<tbody id="filecontent">
                	<?php if($files):?>
						<?php foreach($datas as $n => $role_datas): ?>
							<tr>
						<td><?php echo $role_datas['num']; $titleName = urlencode($role_datas['name_cn']);?></td>
						<td><?php echo $role_datas['size'];?></td>
						<td>
							<a
							href="javascript:void(0);"
							onclick="return show_delete_confirm('<?php echo lang('delete_message');?>','<?php echo lang('delete_message_confirm');?>', '<?php echo site_url($this -> config -> item('admin_folder').'product/deleteFile/'.$pid.'/'.$menu_datas['name_cn'].'/'.$menu_datas['id']); ?>');"
							title="<?php echo lang('menu_edit'); ?>"><span
								title="<?php echo lang('menu_edit'); ?>"
								class="icon  icon-minus"></span></a>

							</td>
						</tr>
					  <?php endforeach; ?>
                <?php else:?>
                    <tr><td colspan="10"><div class="center"><h4>无任何数据</h4></div></td></tr>
                <?php endif;?>
            </table>
		</div>
	</div>
</div>

<div class="modal hide fade" id="myModal" tabindex="-1" role="dialog">
<div class="modal-header"><button class="close" type="button" data-dismiss="modal">×</button>
<h3 id="myModalLabel">文件上传</h3>
</div>
<div class="modal-body"></div>
<div class="box-content">
			<form action="" method="post" accept-charset="utf-8" class="form-horizontal" id="menu_form">						  
			<fieldset>
			<div class="control-group">
					<label class="control-label" for="pid"><?php echo lang('menu_pid'); ?></label>
                <div class="controls">
					<?php
								$data = array (
										'id' => 'pname',
										'name' => 'pname',
										'value' => set_value ( 'pname', '' ),
										'readonly' => 'true'

								);
								echo form_input ( $data );
								
								$data = array (
										'id' => 'pid',
										'name' => 'pid',
										'value' => set_value ( 'pid', $pid ),
										'style' => 'display:none'

								);
								echo form_input ( $data );

								$data = array (
										'id' => 'mid',
										'name' => 'mid',
										'value' => set_value ( 'mid', '' ),
										'style' => 'display:none'

								);
								echo form_input ( $data );
								?>
				<span class="help-inline"><?php echo lang('menu_pid_span'); ?></span>
					</div>
				</div>


				<div class="control-group">
					<label class="control-label" for="name"><?php echo lang('file_path'); ?></label>
					<div class="controls">
						<div id="ossfile" style="overflow:auto;max-height:200px">你的浏览器不支持flash,Silverlight或者HTML5！</div>
						<div id="container">
							<a id="selectfiles" href="javascript:void(0);" class='btn'>选择文件</a>
							<a id="postfiles" href="javascript:void(0);" class='btn'>开始上传</a>
						</div>

					</div>
				</div>


				<div class="form-actions">
					<a id="submit" class="btn btn-primary"><?php echo lang('button_save'); ?></a>
				</div>
			</fieldset>
			</form>
</div>
<!--角色列表结束-->
<?php
echo $this->load->view ( 'footer' );
?>
<script type="text/javascript" src="<?php echo SITE_ADMIN_JS; ?>/crypto1/crypto/crypto.js"></script>
<script type="text/javascript" src="<?php echo SITE_ADMIN_JS; ?>/crypto1/hmac/hmac.js"></script>
<script type="text/javascript" src="<?php echo SITE_ADMIN_JS; ?>/crypto1/sha1/sha1.js"></script>
<script type="text/javascript" src="<?php echo SITE_ADMIN_JS; ?>/base64.js"></script>
<script type="text/javascript" src="<?php echo SITE_ADMIN_JS; ?>/plupload-2.1.2/js/plupload.full.min.js"></script>
<script type="text/javascript" src="<?php echo SITE_ADMIN_JS; ?>/upload.js"></script>

<script type="text/javascript">

function updateParentNode(node,count){

		console.log(node)
		if (node == null || node.treegrid("getParentNode")== null) {
			return;
		};
		var item = node.treegrid("getParentNode").find('.count')
		console.log(item);
		item.html(parseInt(item.html())+count)
		updateParentNode(item,count)
}

$(document).ready(function() {
        $('.tree').treegrid({initialState: 'collapsed'});

        $(".menus").click(function(){
			$(".select-tr").removeClass("select-tr");
        	$(this).parent().addClass("select-tr");

        	var url = "<?php echo site_url($this -> config -> item('admin_folder').'product/files/'.$pid.'/');?>";
        	url = url + "/" + $(this).attr("data");
        	$.post(url, function(data) {
				$("#filecontent").empty()

				if (data == null || data.length == 0) {
					$("#filecontent").append('<tr><td colspan="10"><div class="center"><h4>无任何数据</h4></div></td></tr>');
					return;
				};

				for (var i = 0; i < data.length; i++) {
					$("#filecontent").append('<tr><td>'+data[i].name+'</td><td>'+data[i].size+'</td><td><a href="javascript:void(0);" class="delete" data="'+data[i].id+'"><span class="icon  icon-minus"></span></a></td></tr>'); 
				};

			});
		})

		 $('#submit').click(function() {
		 	if (!$(this).hasClass("btn-primary")) {
		 		return;
		 	};
			var url= "/admin/product/addfile";
		 	$.post(url,$("#menu_form").serialize(), function(data) {

        		$('#myModal').modal('hide');

				var url = "<?php echo site_url($this -> config -> item('admin_folder').'product/files/'.$pid.'/');?>";
	        	url = url + "/" + $("#mid")[0].value;
	        	$.post(url, function(data) {
					$("#filecontent").empty()

					if (data == null || data.length == 0) {
						$("#filecontent").append('<tr><td colspan="10"><div class="center"><h4>无任何数据</h4></div></td></tr>');
						var count = parseInt($(".select-tr .count").html())
						$(".select-tr .count").html("0")
						updateParentNode($(".select-tr .count"), count)
						return;
					};

					for (var i = 0; i < data.length; i++) {
						$("#filecontent").append('<tr><td>'+data[i].name+'</td><td>'+data[i].size+'</td><td><a href="javascript:void(0);" class="delete" data="'+data[i].id+'"><span class="icon  icon-minus"></span></a></td></tr>'); 
						
					};
					var count = parseInt($(".select-tr .count").html())
					$(".select-tr .count").html(data.length )
					updateParentNode($(".select-tr"), data.length - count)
				});
			});
		 });

		$(".expand_menu").click(function(){
        	$(this).treegrid('toggle');
		})

		$(".addmenu").click(function(){
			$(".select-tr").removeClass("select-tr");
        	$(this).parent().parent().addClass("select-tr");
			$('#mid')[0].value = $(this).attr('data');
			$('#pname')[0].value = $(this).attr('dataname');
			$('#ossfile').empty();
        	$('#myModal').modal('show');
		})

		$("td .delete").live("click",function(){
			var node = $(this).parent().parent();
			var id = $(this).attr("data");
			$.dialog({
				title : "提示",
				content : "确定要删除？",
				lock : true,
				fixed : true,
				ok : function() {
					$.post("/admin/product/deleteFile/"+id, function(data) {
						var message_data = $.parseJSON(data);
						if (message_data['msg'] == 1) {
							node.remove();
							$(".select-tr .count").html($("#filecontent tr").length)
						updateParentNode($(".select-tr"), -1)

						} else {
							show_dialog(message_data['info']);
						}
					});
				},
				okValue : '确定',
				cancel : true,
				cancelValue : '关闭'
			});
		});
    });
</script>
</body>
</html>
